package cn.com.shopec.erp.purchase.service.imp;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.shopec.erp.common.constant.ProductSCMConstant;
import cn.com.shopec.erp.common.constant.SZSCMConstant;
import cn.com.shopec.erp.common.page.PageFinder;
import cn.com.shopec.erp.common.page.PageQuery;
import cn.com.shopec.erp.common.utils.DateUtil;
import cn.com.shopec.erp.common.utils.LogicUtil;
import cn.com.shopec.erp.common.utils.StringUtil;
import cn.com.shopec.erp.product.entity.Product;
import cn.com.shopec.erp.purchase.commo.PurchaseRequisitionProductVoConvertor;
import cn.com.shopec.erp.purchase.dao.IPurchaseOrderDao;
import cn.com.shopec.erp.purchase.dao.IPurchaseRequisitionProductDao;
import cn.com.shopec.erp.purchase.entity.PurchaseOrder;
import cn.com.shopec.erp.purchase.entity.PurchaseRequisitionProduct;
import cn.com.shopec.erp.purchase.entity.PurchaseReturnOrder;
import cn.com.shopec.erp.purchase.service.IPurchaseOrderService;
import cn.com.shopec.erp.purchase.service.IPurchaseRequisitionService;
import cn.com.shopec.erp.purchase.vo.PurchaseOrderVo;
import cn.com.shopec.erp.purchase.vo.PurchaseRequisitionProductVo;

@Service
public class PurchaseRequisitionServiceImpl implements IPurchaseRequisitionService{

	@Resource
	private IPurchaseRequisitionProductDao purchaseRequisitionDao;
	
	@Resource
	private PurchaseRequisitionProductVoConvertor purchaseRequisitionProductVoConvertor;
	
	@Resource
	private IPurchaseOrderDao purchaseOrderDao;
	
	@Resource
	private IPurchaseOrderService purchaseOrderService;
	/**
     * 
     * @Title: purchaseOrderListToWarehouse
     * @Description: 采购商品入库
     * @param PurchaseOrderVo
     * @param pageQuery
     * @return
     * @throws
     * @author: LiangShunWang
     * @date: 2016-12-1下午16:52:05
     */
	@Override
	public PageFinder<PurchaseRequisitionProductVo> purchaseOrderListToWarehouse(PurchaseRequisitionProductVo purchaseOrderVo,
			PageQuery pageQuery) throws Exception {
		DetachedCriteria criteria = this.purchaseRequisitionDao.createCriteria();
		if(purchaseOrderVo!=null){

			//已采购单号做查询条件
			if(!StringUtil.isBlank(purchaseOrderVo.getPoNo())){
				criteria.add(Restrictions.ilike("poNo", purchaseOrderVo.getPoNo().trim(),MatchMode.ANYWHERE));
			} 
		}

        criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO)); //未删除
		//criteria.addOrder(Order.desc("foPurDate")); //默认按预定采购日期排序
		PageFinder<PurchaseRequisitionProduct> pagedBrandList = this.purchaseRequisitionDao.pagedByCriteria(criteria, pageQuery.getPage(),pageQuery.getPageSize());
		return this.purchaseRequisitionProductVoConvertor.convert2PagedDefectiveVos(pagedBrandList);
    }
	
	/**
     * 
     * @Title: updateBrand
     * @Description: 修改采購活潑信息
     * @param brandVo
     * @return
     * @throws
     * @author: duhui
     * @date: 2013-5-26下午04:17:40
     */
	@Override
	public boolean updatePurchaseOrder(List<PurchaseRequisitionProductVo> list,String poNo) {
		boolean flag=false;
		Double totalMoney = 0.00;
		try {
			for (PurchaseRequisitionProductVo pur : list) {
				totalMoney = totalMoney+Double.parseDouble(pur.getTotalPrice());
				
				purchaseRequisitionDao.updatePurchaseOrder(pur);
			}
			//修改采购单总价格
			if(purchaseOrderService.updatePurchaseOrderTotalPrice(poNo,totalMoney)){
				flag = false;
			}
			flag=true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	
	/**
     * 
     * @Title: purchaseReturnListToWarehouse
     * @Description: 采购单退货
     * @param brandVo
     * @return
     * @throws
     * @author: duhui
     * @date: 2013-5-26下午04:17:40
     */
	@Override
	public PageFinder<PurchaseRequisitionProductVo> purchaseReturnListToWarehouse(
			PurchaseRequisitionProductVo purchaseOrderVo, PageQuery pageQuery) throws Exception {
		int count = purchaseOrderDao.getProductsCountByStockNo(ProductSCMConstant.PO_STATUS_SEX);
		PageFinder<PurchaseRequisitionProductVo> result = new PageFinder<PurchaseRequisitionProductVo>(pageQuery.getPage(),pageQuery.getPageSize(), count);
		if (count > 0) {
			List<PurchaseRequisitionProductVo> stockOutVos = purchaseRequisitionDao.queryPurchaseList(ProductSCMConstant.PO_STATUS_SEX,result.getStartOfPage(), result.getPageSize());
			result.setData(stockOutVos);
		}
		return result;
	}
	
	/**
     * 
     * @Title: purchaseReturnListToWarehouse
     * @Description: 修改采购单货品的退货状态码
     * @param brandVo
     * @return
	 * @throws Exception 
     * @throws
     * @author: duhui
     * @date: 2013-5-26下午04:17:40
     */
	@Override
	@Transactional
	 public boolean updatePurchaseRequisition(PurchaseReturnOrder purchase) throws Exception{
	    boolean	bool = false;
		if (purchase!=null) {
			List<PurchaseRequisitionProduct> requistion = purchaseRequisitionDao.queryPoNoPurchaseList(purchase);
			for (PurchaseRequisitionProduct element : requistion) {
				if(element.getRetunStatus()==0){
					element.setRetunStatus(ProductSCMConstant.PURCHASE_RETURN_STATUS_FIRST);
					this.purchaseRequisitionDao.update(element);
					 return bool = true;
				}else if(element.getRetunStatus()==1){
					element.setRetunStatus(ProductSCMConstant.PURCHASE_RETURN_STATUS_ZERO);
					this.purchaseRequisitionDao.update(element);
					 return bool = true;
				}
			}
			
		}
		return bool;
	 }
	
	/*@Override
	public boolean updatePurchaseOrderTotalPrice(String purchaseNo,Double price) throws Exception {
		boolean bool = false;
		if (LogicUtil.isNotNull(purchaseNo)) {
			PurchaseOrder purchase = this.poDao.get("poNo",purchaseNo);
			purchase.setTotalPrice(price);
			this.poDao.update(purchase);
			bool = true;
		}
		return bool;
		
	}*/
	

	/**
	 * 根据采购单id查询采购单货品信息
	 */
	@Override
	public PurchaseRequisitionProduct getPurchaseRequistionById(String prqNo) {
		if(prqNo!=null){
			PurchaseRequisitionProduct product = null;
			    DetachedCriteria criteria = this.purchaseRequisitionDao.createCriteria();
			    criteria.add(Restrictions.eq("id", prqNo));
			    criteria.add(Restrictions.eq("deleteFlag", SZSCMConstant.DELETE_FLAG_NO));
			    List<PurchaseRequisitionProduct> productList = this.purchaseRequisitionDao.findByCriteria(criteria);
			    if (LogicUtil.isNotNullAndEmpty(productList)) {
			        product = productList.get(0);
			    }
			    return product;
		}
		return null;

	}

	@Override
	public Boolean updateAuditResultPurchaseRequestion(String purchaseNo, String prodNo) throws Exception {
		Boolean bool = false;
		if(!purchaseNo.equals("") && purchaseNo!=null || !prodNo.equals("") && prodNo!=null){
			PurchaseReturnOrder purchase = new PurchaseReturnOrder();
			purchase.setPoNo(purchaseNo);
			purchase.setProdNo(prodNo);
			List<PurchaseRequisitionProduct> requistion = purchaseRequisitionDao.queryPoNoPurchaseList(purchase);
			for (PurchaseRequisitionProduct element : requistion) {
				if(element!=null){
					element.setRetunStatus(ProductSCMConstant.PURCHASERETURN_STATUS_FIRST);
					this.purchaseRequisitionDao.update(element);
				}else{
					return bool;
				}
			}
		}
		
		return bool;
	}
	
	
	/**
     * 
     * @Title: purchaseOrderListToExpired
     * @Description: 获取过期的货品
     * @param PurchaseOrderVo
     * @param pageQuery
     * @return
     * @throws
     * @author: LiangShunWang
     * @date: 2016-12-1下午16:52:05
     */
	@Override
	public PageFinder<PurchaseRequisitionProduct> purchaseOrderListToExpired(PurchaseRequisitionProduct purchaseRequisitionProduct,
			PageQuery pageQuery) throws Exception {
		return purchaseRequisitionDao.purchaseOrderListToExpired(purchaseRequisitionProduct,pageQuery);
    }
	
	
	/**
     * 
     * @Title: purchaseOrderListToExpired
     * @Description: 获取库存报警的货品
     * @param PurchaseOrderVo
     * @param pageQuery
     * @return
     * @throws
     * @author: LiangShunWang
     * @date: 2016-12-1下午16:52:05
     */
	@Override
	public PageFinder<PurchaseRequisitionProduct> purchaseOrderListToInventoryAlarm(PurchaseRequisitionProduct purchaseRequisitionProduct, PageQuery pageQuery)throws Exception{
		return purchaseRequisitionDao.purchaseOrderListToInventoryAlarm(purchaseRequisitionProduct,pageQuery);
	}

	

		
}
